/**
 * <p>文件名:		MonitorClient.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		CompanyTag</p>
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 */

package com.caits.lbs.framework.services.mina;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.caits.lbs.framework.log.CommonLogFactory;

/**
 * <p>
 * MonitorClient
 * </p>
 * <p>
 * 监控平台指令实现类
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-3-22 下午03:46:40</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
public class MonitorClient implements IClientService {

	/** TODO */
	private Logger log = CommonLogFactory.getLog();
	/** 用户名 */
	private String user;
	/** 密码 */
	private String password;
	/** 任务队列缓存，接收到消息先缓存再处理 */
	private static BlockingQueue<Message> quene = new LinkedBlockingQueue<Message>();
	
	/* 监控平台登录数据
	 * (non-Javadoc)
	 * 
	 * @see com.caits.lbs.framework.services.mina.IClientService#login()
	 */
	public String login() {
		return "LOGI UTOO "+user+" "+password;
	}

	/* 监控平台回复给服务器的数据
	 * (non-Javadoc)
	 * 
	 * @see com.caits.lbs.framework.services.mina.IClientService#writeData()
	 */
	public String getData() {
		return "CAITS ...";
	}

	/* 监控平台退出指令
	 * (non-Javadoc)
	 * @see com.caits.lbs.framework.services.mina.IClientService#logout()
	 */
	public String logout() {
		// TODO Auto-generated method stub
		return "LOGOUT";
	}

	/**
	 * 获取属性user的值
	 * @return the user : String
	 */
	public String getUser() {
		return user;
	}

	/**
	 * 设置属性 user 的值，String类型
	 * @param user the user to set
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * 获取属性password的值
	 * @return the password : String
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * 设置属性 password 的值，String类型
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/* (non-Javadoc)
	 * @see com.caits.lbs.framework.services.mina.IClientService#handleMessage(org.apache.mina.core.session.IoSession, java.lang.Object)
	 */
	@Override
	public Object handleMessage(IoSession session, Object message) {
		Message mess = (Message)message;
		log.debug("收到数据:"+message);
		String result = "缓存成功";
		//注意add,offer,put的区别
		boolean access = quene.offer(mess);
		if(!access) result = "缓存失败";
		return result;
	}

	/**
	 * 外部调用来处理消息 
	 * @return
	 */
	public static Object takeMessage(){
		try {
			return quene.take();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		return null;
	}
}
